package com.gsmr.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gsmr.entity.FrequencySpectrum;
import com.gsmr.entity.Signal_overall;
import com.gsmr.service.FrequencySpectrumService;
import com.gsmr.service.Signal_overallService;
import com.gsmr.util.DateUtil;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Controller
@RequestMapping("/frequencySpectrum")
public class FSController {
	FrequencySpectrum frequencySpectrum;
	String JSONStr;
	File file;
	Queue<FrequencySpectrum> queue = new LinkedList<FrequencySpectrum>();
	
	@Resource
	private FrequencySpectrumService frequencySpectrumService;
	
	@Resource
	private Signal_overallService signal_overallService;
	
	/**
	 * 查询频谱
	 * @param startTime
	 * @param endTime
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,
			@RequestParam(value="frontProcessorId",required=false)String frontProcessorId,
			@RequestParam(value="startTime",required=false)String startTime,
			@RequestParam(value="endTime",required=false)String endTime,
			FrequencySpectrum frequencySpectrum,HttpServletResponse response)throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("frontProcessorId", frontProcessorId);
		//map.put("start", PropertityUtil.FrequencySpectrum_DATA_STARTINDEX++);
		//map.put("size", 1);
		map.put("database_name", PropertityUtil.GSMR_DATABASE);
		map.put("table_name", "fs_signal_overall");
		map.put("signal_type", "FrequencySpectrum");
		if(queue==null||queue.size()<=0){
			List<String> table_nameList = frequencySpectrumService.findTableNames(map);
			List<FrequencySpectrum> frequencySpectrumList = new LinkedList<FrequencySpectrum>();
			if(table_nameList.size()>0){
				for(String table_name : table_nameList){
					map.put("table_name", table_name);
					List<FrequencySpectrum> sub_frequencySpectrumList = frequencySpectrumService.list(map);
					frequencySpectrumList.addAll(sub_frequencySpectrumList);
				}
			}
			queue.addAll(frequencySpectrumList);
		}
		
		//Long total=frequencySpectrumService.getTotal(map);
		JSONObject result=new JSONObject();
		List<FrequencySpectrum> tmpList = new ArrayList<FrequencySpectrum>();
		tmpList.add(queue.poll());
		JSONArray jsonArray=JSONArray.fromObject(tmpList);
		//String datas1 = "{\"total\":28,\"rows\":[{\"frontProcessorID\":\"111\",\"recordID\":\"Koi\",\"downGain\":10.00,\"upGain\":\"P\",\"timeStamp\":36.50,\"timeStampStr\":\"Large\",\"cellInfo\":\"EST-1\"}]}";
		//result = (JSONObject) JSONSerializer.toJSON(datas1);  
		result.put("rows", jsonArray);
		//result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 查找前置机列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/frequencySpectrumFrontProcessorIdComboList")
	public String frequencySpectrumFrontProcessorIdComboList(HttpServletResponse response)throws Exception{
		JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("database_name",  PropertityUtil.GSMR_DATABASE);
		map.put("table_name", "fs_signal_overall");
		map.put("signal_type", "FrequencySpectrum");
		List<Signal_overall> signal_overallList = signal_overallService.findFrontProcessorIds(map);
		JSONArray rows=JSONArray.fromObject(signal_overallList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;
	}
	
	
	public boolean readFSContent(File file){
		this.file = file;
		Reader reader = null;
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			int tempchar;
			StringBuffer sb = new StringBuffer();
			
			while ((tempchar = reader.read()) != -1) {
				sb.append((char)tempchar);
			}
			if(sb.length()>0){
				this.JSONStr = sb.toString();
				reader.close();
				return true;
			}else{
				return false;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		
	}

	public FrequencySpectrum ParseFrequencySpectrum(){
		frequencySpectrum = new FrequencySpectrum();
		JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(this.JSONStr);
		
		String frontProcessorID = jsonObject.getString("FrontProcessorID");
		String downGain = jsonObject.getString("DownGain");
		String upGain = jsonObject.getString("UpGain");
		String timeStamp = jsonObject.getString("TimeStamp");
		String data = jsonObject.getString("Data");
		String cellInfo = jsonObject.getString("CellInfo");
		
//		frequencySpectrum.setCellInfo(cellInfo);
//		frequencySpectrum.setData(data);
//		frequencySpectrum.setDownGain(downGain);
//		frequencySpectrum.setFrontProcessorID(frontProcessorID);
		frequencySpectrum.setTimeStamp(DateUtil.stringToLong(timeStamp,"yyyy-MM-dd HH:mm:ss SSS"));
//		frequencySpectrum.setUpGain(upGain);
//		frequencySpectrum.setTimeStampStr(timeStamp);
//		frequencySpectrum.setRecordID(getRecordIdFromFileName(file));
		
		return frequencySpectrum;
	}
	
	static String getRecordIdFromFileName(File file){
		String recordId = null;
		if(file!=null){
			String fileName = file.getName();
			try {
				recordId = fileName.substring(fileName.indexOf("-")+1,fileName.lastIndexOf("_"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return recordId;
		}else{
			return null;
		}
		
	}
	
	public int insert(FrequencySpectrum frequencySpectrum){
		ArrayList<FrequencySpectrum> arrayList = new ArrayList<FrequencySpectrum>();
		arrayList.add(frequencySpectrum);
		
		Map<String,Object> hm = new HashMap<String,Object>();
		hm.put("database_name", PropertityUtil.GSMR_DATABASE);
		String fileName = file.getName();
		String table_name = "frequencyspectrum_"+fileName.substring(0,fileName.indexOf("-"));
		hm.put("table_name", table_name);
		hm.put("list", arrayList);
		frequencySpectrumService.create_frequencySpectrum_table(hm);
		int flag = frequencySpectrumService.insert_FrequencySpectrum_batch(hm);
		return flag;
	}
	
}
